package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.jqgrid.JqGridFilter;
import com.fz.us.admin.base.bean.jqgrid.JqGridPager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PageHelp;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.company.bean.FlowEnum;
import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.*;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.admin.dict.service.DictItemService;
import com.fz.us.oa.core.entity.com.NoticeQuery;
import com.fz.us.oa.core.entity.com.NoticeTest;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.core.service.com.NoticeQueryService;
import com.fz.us.oa.core.service.com.NoticeTestService;
import com.fz.us.oa.core.service.common.FileManagerService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.fz.us.oa.web.spring.bind.annotation.RequestJsonParam;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 控制器 - 公告通知登记表测试
 * ============================================================================
 * 版权所有 2016 。
 *
 * @version 1.0 2016-12-28 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/com/running")
public class RunningController extends BaseController {


    private static final long serialVersionUID = 715013299230114569L;
    @Autowired
    private TaskEntityService taskEntityService;
    @Autowired
    private TaskFlowService taskFlowService;
    @Autowired
    private ProcessBriefService processBriefService;
    @Autowired
    private StepBriefService stepBriefService;
    /**
     * 流程信息
     * @return
     */
    @RequestMapping(value = "workflow", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String workflow(@CurrentUser User user, Model model,String bussinessId,String type){
        TaskEntity taskEntity = taskEntityService.getInfoByKeyId(bussinessId);
        List<TaskFlow> taskFlowList = Lists.newArrayList();
        Set<User> userSet = Sets.newHashSet();
        if(taskEntity == null){
            if(StringUtils.equals("next", type)){
                return "/com/running_next";
            }
            return "/com/running_workflow";
        }
        taskFlowList = taskFlowService.getInfoByTaskEntity(taskEntity);
        ProcessBrief processBrief = processBriefService.getInfoByTableKey(taskEntity.getEntityName());
        StepBrief stepBrief = stepBriefService.getInfoByStepNo(processBrief, taskEntity.getStepNo());
        if(stepBrief != null &&
                (StringUtils.equals("Running", taskEntity.getProcessState().toString())||
                        StringUtils.equals("Backed", taskEntity.getProcessState().toString()) ||
                        StringUtils.equals("Draft", taskEntity.getProcessState().toString()))){
            userSet =  taskEntity.getEditors();
        }
        model.addAttribute("taskFlowList", taskFlowList);
        model.addAttribute("userSet", userSet);
        if(StringUtils.equals("next", type)){
            return "/com/running_next";
        }
        return "/com/running_workflow";
    }


    /**
     * 计算权限
     * @return
     */
    @RequestMapping(value = "operateType", method = RequestMethod.POST)
    public Object operateType(@CurrentUser User user,String keyId){
        Map<String,Object> rMap=new HashMap<String,Object>();
        TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
        Set<User> userSet = taskEntity.getEditors();
        boolean flag = false;
        for(User user1:userSet){
            if(user1 == user){
                flag = true;
                break;
            }
        }
        if(!flag){
            rMap.put("state","400");
            return ajaxJson(rMap);
        }
        List<JSONObject> datarow = Lists.newArrayList();
        Map<String, Object> data = new HashMap<String, Object>();
        ProcessBrief processBrief = processBriefService.getInfoByTableKey(taskEntity.getEntityName());
        StepBrief stepBrief = stepBriefService.getInfoByStepNo(processBrief, taskEntity.getStepNo());
        if(stepBrief != null){
            rMap = Maps.newHashMap();
            rMap.put("action", "approve");
            rMap.put("name", "通过");
            datarow.add(JSONObject.fromObject(rMap));
            if(StringUtils.indexOf(stepBrief.getActionType(),"1") != -1){
                rMap = Maps.newHashMap();
                rMap.put("action", "sendback");
                rMap.put("name", "退回");
                datarow.add(JSONObject.fromObject(rMap));
            }
            if(StringUtils.indexOf(stepBrief.getActionType(),"2") != -1){
                rMap = Maps.newHashMap();
                rMap.put("action", "deny");
                rMap.put("name", "否决");
                datarow.add(JSONObject.fromObject(rMap));
            }
        }
        data.put("datarows", datarow);
        data.put("state","1");
        return ajaxJson(data);
    }

}
